/*
 * IncFile1.h
 *
 * Created: 5/21/2011 5:15:41 PM
 *  Author: X-Ravin
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/boot.h>
#include <avr/pgmspace.h>
#include <inttypes.h>
#include "isr_vectors.h"
#include "kernel_io.h"
#include "strings.h"


#define FREE 0
#define RUNNING 1
#define RUNNABLE 2
#define BLOCKED 3
#define QUEUED 4
#define FINISHED 5
#define ERROR 65535


#define ERROR_PROC_ALLOC 0xF0

#define NUM_PROCS 5

#define USER_SPACE_OFFSET 512
#define FREE_FLASH_START 4096

void writeFlashProgram(char progNumber, unsigned int size) BOOTLOADER_SECTION;
void verifyProg(char proc);
char findFreeProcess(unsigned int size);
unsigned int freeRamSpace(char proc);
unsigned int freeFlashSpace(char prog);
void allocProgram(char prog, unsigned int size);
void installProgram(void);
char findFreeProgram(unsigned int size);
void list(void);
void runProgram(char prog);
void allocProcess(char proc, unsigned int size);
char preallocProgram(unsigned int flashSize, unsigned int ramSize);
void puts_f(char i);
void returnState(void);
void saveState(void);
void stopScheduler(void);
void startScheduler(void);
void handleISR(const int address, char owner);


struct proc_block
{
	char program;
	unsigned int size;
	unsigned int heap_start_address;
	unsigned int stack_pointer;
	unsigned int owned_resources;
	char state;
};

struct program_block
{
	unsigned int offset_address; //Code start
	unsigned int size;	//.text + .data
	char PID;	//Which proc is it in
	char state;	//Runnable or free
	char ID;	//ID of the program, so user can know which program it is, this is user defined.
};



extern char current_proc;
extern struct proc_block process[];
extern struct program_block program[];
extern char isr[];
extern unsigned int kernel_sp, last_proc_sp;
